package cn.xenosp.server.config;

import cn.xenosp.server.common.cache.Cache;
import cn.xenosp.server.common.cache.impl.InsideCache;
import cn.xenosp.server.common.cache.impl.RedisCache;
import cn.xenosp.server.common.sslApply.CertbotManager;
import cn.xenosp.server.common.system.TargetOperatingSystem;
import cn.xenosp.server.common.system.impl.LinuxSystem;
import cn.xenosp.server.common.system.impl.WindowsSystem;
import cn.xenosp.server.common.tools.PropertiesUtil;
import cn.xenosp.server.common.tools.ServiceUtil;
import cn.xenosp.server.common.tools.SpringBeanUtil;
import cn.xenosp.server.common.tools.systemInfo.RedisInfoUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import oshi.SystemInfo;
import oshi.software.os.OperatingSystem;

import javax.annotation.PostConstruct;
import java.util.Arrays;


/**
 * 项目名： nginx-web
 * 包路径： cn.xenosp.nginx.config
 * 作者：   TongHui
 * 创建时间: 2024-07-25 16:37
 * 描述: TODO
 * 版本: 1.0
 */
@Slf4j
@Configuration
public class BeanConfig {

    @Value("${properties:}")
    String properties;
    @Value("${app.config.cache-type:inside}")
    String cacheType;


    @PostConstruct
    public void initConfig(){
        log.info("======================初始化配置=========================");
        // 初始化工具类
        PropertiesUtil.instance(Arrays.asList(properties.split(",")));
    }

    @Bean
    public TargetOperatingSystem createSystem(){
        SystemInfo systemInfo = new SystemInfo();
        OperatingSystem operatingSystem = systemInfo.getOperatingSystem();
        OperatingSystem.OSVersionInfo osVersionInfo = systemInfo.getOperatingSystem().getVersionInfo();
        log.info("初始化系统类型====="+SystemInfo.getCurrentPlatform());
        log.info("初始化系统类型====="+operatingSystem.getFamily() + " " + osVersionInfo.getVersion());
        if (ServiceUtil.isWINDOWS()){
            return new WindowsSystem();
        }
        if (ServiceUtil.isLINUX()){
            return new LinuxSystem();
        }
        throw new IllegalStateException(SystemInfo.getCurrentPlatform() + "没有实现该系统");
    }



    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory connectionFactory) {
        log.info(" --- redis config init --- ");
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(connectionFactory);
        template.setKeySerializer(new StringRedisSerializer());
        template.setValueSerializer(new GenericJackson2JsonRedisSerializer());

        RedisInfoUtil.redisTemplate = template;
        RedisInfoUtil.redisConnectionFactory = connectionFactory;
        return template;
    }

    /**
     * Spring上下文工具配置
     *
     * @return
     */
    @Bean
    @ConditionalOnMissingBean(SpringBeanUtil.class)
    public SpringBeanUtil springContextHolder() {
        SpringBeanUtil holder = new SpringBeanUtil();
        return holder;
    }

    /**
     * 缓存初始化
     */
    @Bean
    public Cache cache(RedisTemplate<String, Object> redisTemplate){
        log.info("初始化缓存：{}",cacheType);
        if (Cache.REDIS_CACHE.equals(cacheType)){
            RedisCache redisCache = new RedisCache();
            redisCache.setRedisTemplate(redisTemplate);
            return redisCache;
        }
        return new InsideCache();
    }

}
